<?php

	session_start();
	require("connect.php");
	$link = connect_APO();
		
	require("check.php");
	is_logged_in();
	is_admin();
	$users = $_POST["credit"];
	
	
	if($users == null){
		// redirect to error page
	}
	$message = "";
		
	if ($_POST['submitPending'] == 'Grant Credit'){
		$message = "Pending credit has been granted for the following selected users:</br></br>";
		$title = "Credit Granted";
		$sql = "UPDATE `users` SET `TotCredit` = (Pending + TotCredit), Pending = 0, PendingLocation = '' WHERE CONVERT( `users`.`ID` USING utf8 ) in (";
	} elseif($_POST['submitPending'] == 'Clear Pending Credit'){
		$message = "Pending credit has been cleared for the following selected users:</br></br>";
		$title = "Pending Credit Cleared";
		$sql = "UPDATE `users` SET Pending = 0, PendingLocation = '' WHERE CONVERT( `users`.`ID` USING utf8 ) in (";
	}
	
	for($i=0; $i<sizeof($users); $i++){
			$message .= $users[$i]."<br>";
			$sql .= "'".mysql_real_escape_string($users[$i])."',";
	}

	$sql = substr($sql,0,strlen($sql)-1).");";
	
	$message .= $messageEnd;
	
	$result = mysql_query($sql);
	
	$test = mysql_affected_rows();
	
	require("header+logoutB.php");
	headerL();
	
	
	if(0 >= $test){
		$message = "There was an error with your request. Please try again or just add the credit to the appropriate users.";
	}
		
	echo "<html>
	<link rel='stylesheet' type='text/css' href='webstyle.css' />
	<head>
		<title>$title</title>
		<meta http-equiv='refresh' content='10; URL=pendingAdmin.php'>
	</head>
	<body>
	<center>
	<font class='mainFont'> $message </br> You will be redirected back to the pending credit page in 10 seconds or click the following link <A href='pendingAdmin.php'>Back to Pending Credit for Users</A></font>
	</center>
	</body>
	</html>";
	
	mysql_close($link);
	
?>